using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace ChatClient
{
    public partial class LogInDialog : Form
    {
        public LogInDialog()
        {
            InitializeComponent();
        }
        /// <summary>
        /// a constructor designed to save the user info that was last used
        /// </summary>
        /// <param name="userName">User Name</param>
        /// <param name="port">Port Number</param>
        /// <param name="address">IP Adress to connect to</param>
        /// <param name="color">User Text Color</param>
        public LogInDialog(string userName, int port, IPAddress address, Color color)
        {
            InitializeComponent();

            if (userName!=null)
            {
                txtUserName.Text = userName;
            }

            if (address!=null)
            {
                txtIpAddress.Text = address.ToString();
            }

            if (color!=null)
            {
                txtColor.BackColor = color;
            }

            if (port > 0 && port < 65535)
            {
                numericPort.Value = port;
                
            }
            
        }

        #region Propeties and data members

        private DialogResult dr;

        public DialogResult LogInResult
        {
            get { return dr; }
        }

        private int port;

        public int Port
        {
            get { return port; }
        }

        private IPAddress address;

        public IPAddress Address
        {
            get { return address; }
        }

        private string userName;

        public string UserName
        {
            get { return userName; }
        }

        private Color color;

        public Color TextColor
        {
            get { return color; }
        }


        # endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dr = DialogResult.Cancel;
            this.Close();
            
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            //checks if a user name has been entered
            if (txtUserName.Text==string.Empty)
            {
                MessageBox.Show("Please enter name");
                return;
            }
            try
            {
                //transfers the data from the form to the
                //data members
                address = IPAddress.Parse(txtIpAddress.Text);
                color = txtColor.BackColor;
                port = (int)numericPort.Value;
                userName = txtUserName.Text;
            }
            catch (FormatException exp)
            {
                MessageBox.Show("IP Address not valid\ntry again.",exp.Message);
                return;
            }
            catch (Exception exp)
            {

                MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            dr = DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// opens a color dialog to set the user's
        /// text color
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog()==DialogResult.OK)
            {
                if (colorDialog1.Color!=Color.White)
                {
                    txtColor.BackColor = colorDialog1.Color;
                    color = colorDialog1.Color;
                }
                else MessageBox.Show("Not White Please");
            }
        }

        

        
    }
}